#!/bin/sh

echo "[INFO] [$(date '+%Y-%m-%d %H:%M:%S')] application calc-task."

if [ -n "$HZ_GROUP_NAME" ];then
    echo "[INFO] [$(date '+%Y-%m-%d %H:%M:%S')] 更改缓存分组为[$HZ_GROUP_NAME]."
    file='hazelcast-default.xml'
    package='calc-task.jar'
    file_path=$(jar tvf $package | grep $file | awk '{print $NF}')
    jar -xvf $package $file_path
    sed -i "s#mg-prod#$HZ_GROUP_NAME#" $file_path
    if [ -n "$INTERFACE" ]; then
        LOCAL_IP=$(ip a|grep "$INTERFACE$" |awk '{print $2}'| cut -d'/' -f1)
        line=$(grep -rn 'interfaces enabled' $file_path | awk -F ':' '{print $1}')
        sed -i "$line s#false#true#" $file_path
        sed -i "s#10.10.1.*#$LOCAL_IP</interface>#" $file_path
    fi
    jar -uvf  $package $file_path
    rm -rf $(echo $file_path | awk -F '/' '{print $NR}')
fi

if [ -n "$WAIT_HOSTS" ]; then
    echo "[INFO] 检测依赖环境."
    chmod +x /wait
    /wait
fi

exec "$@"
